#include "pwm.h"

void pwm_init(void)
{
    GPIO_InitTypeDef GPIO_init_struct;
    TIM_TimeBaseInitTypeDef timebase_init_struct;
    TIM_OCInitTypeDef OC_init_struct;

     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);/*定时器工作频率为72Mhz*/
     

     GPIO_init_struct .GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_init_struct .GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_init_struct .GPIO_Pin = GPIO_Pin_1;

     GPIO_Init(GPIOA,&GPIO_init_struct);

     timebase_init_struct .TIM_ClockDivision = TIM_CKD_DIV1;
     timebase_init_struct .TIM_CounterMode = TIM_CounterMode_Up;
     timebase_init_struct .TIM_Period = 999;
     timebase_init_struct .TIM_Prescaler = 71;
     //timebase_init_struct .TIM_RepetitionCounter = 0;/*重复计数器，即产生N次溢出事件，才发生一次更新事件*/
     TIM_TimeBaseInit(TIM5, &timebase_init_struct);

     OC_init_struct .TIM_OCIdleState = TIM_OCIdleState_Reset;/*配置定时器在空闲状态或复位时的电平状态*/
     OC_init_struct .TIM_OCMode = TIM_OCMode_PWM1;
     OC_init_struct .TIM_OCPolarity = TIM_OCPolarity_High;
     OC_init_struct .TIM_OutputState = ENABLE;
     OC_init_struct .TIM_Pulse = 900;
     TIM_OC2Init(TIM5, &OC_init_struct);   
     TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);/*使能输出比较预装载，带缓冲*/
     TIM_ARRPreloadConfig(TIM5, ENABLE);/*使能自动重装载预装载，带缓冲*/
     TIM_Cmd(TIM5, ENABLE);
}

